<template>
  <div>
    <el-table
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border

      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        prop="title"
        label="名称"
        sortable
        width="180"
      />
      <el-table-column
        prop="path"
        label="路由地址"
        sortable
        width="180"
      />
      <el-table-column
        prop="icon"
        label="图标"
        width="80"
      >
        <template slot-scope="{row}">
          <img :src="icnoName(row.name)" alt="">
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="标识"
        width="180"
      />
      <el-table-column
        prop="address"
        label="type"
        width="140"
      >
        <template slot-scope="">
          <el-button type="success" plain size="mini">菜单</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        label="权重"
        width="80"
      />
      <el-table-column
        prop="address"
        label="状态"
      >
        <template>
          <el-switch :value="1" active-color="#358cfd" inactive-color="#6eaffe" active-text="显示" inactive-text="禁用" />
        </template>

      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button type="text" size="small" @click="editRow(row.id)">添加子级</el-button>
          <el-button type="text" size="small" @click="editRow(row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="editRow(row.id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

  </div>
</template>
<script>
import iconList from '@/assets/static/icon'
import { getMenuListApi } from '@/api/menu'
import { transListToTreeData } from '@/utils'

export default {
  name: 'TreeTable',
  data() {
    return {
      tableData: [{
        id: 1,
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        id: 2,
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        id: 3,
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄',
        children: [{
          id: 31,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          id: 32,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }]
      }, {
        id: 4,
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }],
      list: []
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    icnoName(icon) {
      return iconList[icon]
    },
    editRow() {

    },
    load(tree, treeNode, resolve) {
      setTimeout(() => {
        resolve([
          {
            id: 31,
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄'
          }, {
            id: 32,
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄'
          }
        ])
      }, 1000)
    },
    // 加载页面数据
    async  getMenuList() {
      const { data } = await getMenuListApi()
      console.log('菜单列表', data)
      this.list = transListToTreeData(data.data, 0)
      console.log(this.list)
    }
  }
}
</script>
